package com.snail.billing.page;

import java.math.BigDecimal;

import org.json.JSONObject;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.PaymentConst;
import com.snail.billing.Resource;
import com.snail.billing.data.PaymentLogic;
import com.snail.billing.json.JsonBase;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.PaymentSession;
import com.tencent.mm.sdk.modelpay.PayReq;

public class PaymentOrderPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private TextView textProduct;
	private TextView textAccount;
	private TextView textPlatform;
	private TextView textPrice;
	private View button;

	private HttpApp httpApp;

	private PaymentSession paymentSession;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_payment_order_activity);
	}

	@Override
	public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
		getPageManager().setContentView(onCreateView(), getLayoutParams());
		onCreate(null);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_payment_order_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		textProduct = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_order_text_product));
		textProduct.setText(ResUtil.getString(Resource.string.snailbilling_payment_order_product) + DataCache.getInstance().importParams.productName);
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_order_text_account));
		textAccount.setText(ResUtil.getString(Resource.string.snailbilling_payment_order_account) + DataCache.getInstance().importParams.account);
		textPlatform = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_order_text_state));
		textPlatform.setText(ResUtil.getString(Resource.string.snailbilling_payment_order_state) + DataCache.getInstance().paymentParams.platformName);
		textPrice = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_order_text_price));
		textPrice.setText(ResUtil.getString(Resource.string.snailbilling_payment_order_price) + new BigDecimal(DataCache.getInstance().importParams.price));
		button = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_payment_order_button));
		button.setOnClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button)) {
			paymentSession = new PaymentSession();
			httpApp.request(paymentSession);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(paymentSession)) {
				String jsonString = (String) httpSession.getResponseData();
				JsonBase result = new JsonBase(jsonString);
				if (result.getCode() == 1) {
					PaymentLogic paymentLogic = new PaymentLogic(getPageManager(), PaymentLogic.STATE_PAYMENT);
					try {
						JSONObject json = new JSONObject(jsonString);
						String orderResponse = "";
						if (json.has("data")) {
							orderResponse = json.getString("data");
						}
						DataCache.getInstance().paymentParams.orderResponse = orderResponse;
						int platformId = DataCache.getInstance().paymentParams.platformId;
						if (platformId == PaymentConst.PAYMENT_TYPE_ALIPAY_APP) {
							JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
							final String source = paymentParams.getString("source");
							//
							paymentLogic.alipayApp(source);
							return;
						}
						if (platformId == PaymentConst.PAYMENT_TYPE_UPOMP) {
							JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
							String unionMobilePayImprestSubmit = paymentParams.getString("unionMobilePayImprestSubmit");
							Log.e("@@@@", unionMobilePayImprestSubmit);
							byte[] to_upomp = unionMobilePayImprestSubmit.getBytes();
							//
							paymentLogic.upompApp(to_upomp);
							return;
						}
						if (platformId == PaymentConst.PAYMENT_TYPE_TENPAY) {
							paymentLogic.tenpayWeb();
							return;
						}
						if (platformId == PaymentConst.PAYMENT_TYPE_WECHAT) {
							JSONObject paymentParams = new JSONObject(orderResponse).getJSONObject("paymentParams");
							PayReq payReq = new PayReq();
							payReq.appId = paymentParams.getString("appid");
							payReq.partnerId = paymentParams.getString("partnerid");
							payReq.prepayId = paymentParams.getString("prepayid");
							payReq.nonceStr = paymentParams.getString("noncestr");
							payReq.timeStamp = paymentParams.getString("timestamp");
							payReq.packageValue = "Sign=WXPay";
							payReq.sign = paymentParams.getString("sign");
							//
							paymentLogic.wxpayApp(payReq);
							return;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					Toast.makeText(getContext(), result.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
